<?php  
//Set values for page
$page_title = "Register";
$current_page = "register";

require_once 'global.inc.php';

//initialize php variables used in the form  
$username = "";
$password = "";
$password_confirm = "";
$email = "";
$error = "";
//check to see that the form has been submitted
if(isset($_POST['submit-form'])) {

	//retrieve the $_POST variables
	$username = $_REQUEST['username'];
	$password = $_REQUEST['password'];
	$password_confirm = $_REQUEST['confirm_password'];
	$email = $_REQUEST['email'];

	$address = $_REQUEST['address'];
	$phone = $_REQUEST['phone'];
	$firstname = $_REQUEST['firstname'];
	$lastname = $_REQUEST['lastname'];
	$company = $_REQUEST['company'];
	$city = $_REQUEST['city'];
	$country = $_REQUEST['country'];
	$postcode = $_REQUEST['postcode'];
	$have_account = 1;




		
	//initialize variables for form validation
	$success = true;
	$userTools = new UserTools();

	//validate that the form was filled out correctly
	//check to see if user name already exists
	if($userTools->checkUserExists($username, $email)) {
		$error .= "That username is already taken.<br/> \n\r";
		$success = false;
	}

	//check to see if passwords match
	if($password != $password_confirm) {
		$error .= "Passwords do not match.<br/> \n\r";
		$success = false;
	}

	if($success) {
		//prep the data for saving in a new user object
		$data['username'] = $username;
		$data['password'] = md5($password); //encrypt the password for storage
		$data['email'] = $email;

		$data['address'] = $address;
		$data['phone'] = $phone;
		$data['firstname'] = $firstname;
		$data['lastname'] = $lastname;
		$data['company'] = $company;
		$data['city'] = $city;
		$data['country'] = $country;
		$data['postcode'] = $postcode;
		$data['have_account'] = $have_account;

		//create the new user object
		$newUser = new User($data);

		//save the new user to the database
		$newUser->save(true);

		//log them in
		$userTools->login($username, $password);

		//redirect them to a welcome page
		header("Location: welcome.php");

	}
}

//If the form wasn't submitted, or didn't validate
//then we show the registration form again
?>
<!DOCTYPE html>
<html lang="en">
	<?php include 'includes/header.php';?>
<body>
	<?php include 'includes/menu.php';?>
	<?php //include 'includes/slideshow.php';?>
	<div class="container" style="margin-top:60px;">
		<div class="row">
			<div class="span12">
				<?php echo ($error != "") ? $error : ""; ?>
				<form action="register.php" method="post">
					<div class="col-1">
						<h3>Register</h3>
						<p>Create an account by entering the information below. If you are a returning customer please login at the top of the page.</p>
						<label class="" for="account_username">Account username</label><input type="text" value="" placeholder="Username" id="account_username" name="username" class="input-text">
						<label class="" for="account_password">Account password</label><input type="password" value="" placeholder="Password" id="account_password" name="password" class="input-text">
						<label class="" for="account_password-2">Confirm password</label><input type="password" value="" placeholder="Confirm password" id="account_password-2" name="confirm_password" class="input-text">
						<label class="" for="billing_email">Email Address </label><input type="text" value="" placeholder="" id="billing_email" name="email" class="input-text">

						<label class="" for="billing_first_name">First Name </label><input type="text" value="" placeholder="" id="billing_first_name" name="firstname" class="input-text">
						<label class="" for="billing_last_name">Last Name </label><input type="text" value="" placeholder="" id="billing_last_name" name="lastname" class="input-text">
						<label class="" for="billing_company">Company Name</label><input type="text" value="" placeholder="" id="billing_company" name="company" class="input-text">
						<label class="" for="billing_address_1">Address </label><input type="text" autocomplete="no" value="" placeholder="Street address" id="billing_address_1" name="address" class="input-text">
						<label class="" for="billing_city">Town / City </label><input type="text" autocomplete="no" value="" placeholder="Town / City" id="billing_city" name="city" class="input-text">
						<label class="" for="billing_state">County </label><input type="text" autocomplete="no" id="billing_state" name="country" placeholder="County" value="" class="input-text">
						<label class="" for="billing_postcode">Postcode</label><input type="text" autocomplete="no" value="" placeholder="Postcode" id="billing_postcode" name="postcode" class="input-text">
						<label class="" for="billing_phone">Phone </label><input type="text" value="" placeholder="" id="billing_phone" name="phone" class="input-text">
						<div>
							<input class="btn" type="submit" value="Register" name="submit-form" />
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<?php include 'includes/footer.php';?>
	<!-- javascript -->
	<script src="js/jquery.js"></script>
	<script src="js/bootstrap.js"></script>
	</body>
</html>